package com.wd.mysquare.view.fragment


import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RadioGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import coil.load
import coil.transform.CircleCropTransformation
import com.ayvytr.adapter.smart
import com.opensource.svgaplayer.SVGAImageView
import com.wd.current.model.Bean.DataBean
import com.wd.live.base.BaseFragment
import com.wd.mysquare.R
import com.wd.mysquare.databinding.FragmentSquareBinding
import com.wd.mysquare.viewmodel.SquareViewModel
import androidx.lifecycle.Observer
import cn.leaqi.drawer.SwipeDrawer
import com.wd.base.App

//附近

class SquareFragment : BaseFragment<SquareViewModel,FragmentSquareBinding>(R.layout.fragment_square) {


    var nearbyLiveAdapter=smart(mutableListOf<DataBean.HomeList.Result>(),
        R.layout.near_list_item,{item, position ->
            findViewById<ImageView>(R.id.headPic).load(item.headPic){
                placeholder(R.mipmap.refresh)
                error(R.mipmap.refresh)
                transformations(CircleCropTransformation())
            }
            findViewById<TextView>(R.id.near_nickName).setText(item.nickName)
            if (item.sex==1){
                findViewById<ImageView>(R.id.near_sex).setImageResource(R.mipmap.man)
            }else{
                findViewById<ImageView>(R.id.near_sex).setImageResource(R.mipmap.woman)
            }

            var liveSVG=findViewById<SVGAImageView>(R.id.near_svg)
            //给SVG设置监听，当显现是开始播放SVG动画
            liveSVG.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
                override fun onViewAttachedToWindow(p0: View) {
                    liveSVG.startAnimation()
                }

                override fun onViewDetachedFromWindow(p0: View) {

                }

            })
        }){}
    override fun dataObserver() {
        viewmodel.findNearbyLiveData.observe(this, Observer {

          nearbyLiveAdapter.update(it.result)


        })
    }

    override fun initViews() {


        binding.rg.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener{
            override fun onCheckedChanged(p0: RadioGroup?, p1: Int) {
                if (R.id.near_all==p1){
                    nearbyLiveAdapter.clear()
                    viewmodel.getNearbyLiveData(App.userId,100,1,10)
                    binding.nearAll.setBackgroundResource(R.drawable.tab_button_bg_select)
                    binding.nearAll.setTextColor(resources.getColor(R.color.white))
                    binding.nearGirl.setBackgroundResource(R.drawable.tab_button_bg)
                    binding.nearGirl.setTextColor(resources.getColor(R.color.se))
                    binding.nearBoy.setBackgroundResource(R.drawable.tab_button_bg)
                    binding.nearBoy.setTextColor(resources.getColor(R.color.se))
                }else if (R.id.near_girl==p1){
                    nearbyLiveAdapter.clear()
                    viewmodel.getNearbyLiveData(App.userId,103,1,10)
                    binding.nearAll.setBackgroundResource(R.drawable.tab_button_bg)
                    binding.nearAll.setTextColor(resources.getColor(R.color.se))
                    binding.nearGirl.setBackgroundResource(R.drawable.tab_button_bg_select)
                    binding.nearGirl.setTextColor(resources.getColor(R.color.white))
                    binding.nearBoy.setBackgroundResource(R.drawable.tab_button_bg)
                    binding.nearBoy.setTextColor(resources.getColor(R.color.se))
                }else if (R.id.near_boy==p1){
                    nearbyLiveAdapter.clear()
                    viewmodel.getNearbyLiveData(App.userId,104,1,10)
                    binding.nearAll.setBackgroundResource(R.drawable.tab_button_bg)
                    binding.nearAll.setTextColor(resources.getColor(R.color.se))
                    binding.nearGirl.setBackgroundResource(R.drawable.tab_button_bg)
                    binding.nearGirl.setTextColor(resources.getColor(R.color.se))
                    binding.nearBoy.setBackgroundResource(R.drawable.tab_button_bg_select)
                    binding.nearBoy.setTextColor(resources.getColor(R.color.white))
                }
            }

        })

        viewmodel.getNearbyLiveData(App.userId,100,1,10)
        binding.nearRv.adapter=nearbyLiveAdapter
        binding.nearRv.layoutManager= LinearLayoutManager(this.context)
        //点击刷新显示列表数据
        binding.refresh.setOnClickListener {
            binding.st.toggleDrawer(SwipeDrawer.DIRECTION_BOTTOM)
            binding.nearRv.visibility=View.VISIBLE
            binding.tab.visibility=View.VISIBLE
            binding.nearSVGOne.visibility=View.VISIBLE
            binding.nearSVGTwo.visibility=View.GONE

        }

    }

}